package com.bm.beautifulmouse.effect;

import com.bm.beautifulmouse.model.EffectConfig;
import javafx.animation.*;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.util.Duration;
import java.util.Random;

public class FirecrackerEffect implements Effect {
    private final Random random = new Random();

    @Override
    public void generate(Pane container, double x, double y, EffectConfig config) {
        // 创建烟花主体
        Circle firecracker = new Circle(3 * config.getSize(), config.getColor());
        firecracker.setCenterX(x);
        firecracker.setCenterY(container.getHeight());

        // 创建上升轨迹
        double targetY = y;
        double duration = 1.0 / config.getSpeed();

        // 烟花上升动画
        Timeline riseTimeline = new Timeline(
                new KeyFrame(Duration.ZERO,
                        new KeyValue(firecracker.centerYProperty(), container.getHeight()),
                        new KeyValue(firecracker.opacityProperty(), 1.0)
                ),
                new KeyFrame(Duration.seconds(duration),
                        new KeyValue(firecracker.centerYProperty(), targetY, Interpolator.EASE_OUT),
                        new KeyValue(firecracker.opacityProperty(), 0.8)
                )
        );

        riseTimeline.setOnFinished(e -> {
            // 移除上升的烟花
            container.getChildren().remove(firecracker);
            // 创建爆炸效果
            createExplosion(container, x, targetY, config);
        });

        container.getChildren().add(firecracker);
        riseTimeline.play();
    }

    private void createExplosion(Pane container, double x, double y, EffectConfig config) {
        int particles = config.getParticleCount();
        double explosionSize = 50 * config.getRange();

        for (int i = 0; i < particles; i++) {
            // 创建爆炸粒子
            Circle particle = new Circle(2 * config.getSize());

            // 设置粒子颜色（随机选择几种喜庆的颜色）
            Color[] festiveColors = {
                    config.getColor(),
                    Color.RED,
                    Color.GOLD,
                    Color.ORANGE,
                    Color.YELLOW
            };
            particle.setFill(festiveColors[random.nextInt(festiveColors.length)]);

            particle.setCenterX(x);
            particle.setCenterY(y);

            // 计算粒子轨迹
            double angle = (360.0 / particles) * i + random.nextDouble() * 30;
            double distance = explosionSize * (0.8 + random.nextDouble() * 0.4);

            // 创建粒子动画
            Timeline particleTimeline = new Timeline(
                    new KeyFrame(Duration.ZERO,
                            new KeyValue(particle.centerXProperty(), x),
                            new KeyValue(particle.centerYProperty(), y),
                            new KeyValue(particle.opacityProperty(), 1.0),
                            new KeyValue(particle.radiusProperty(), 2 * config.getSize())
                    ),
                    new KeyFrame(Duration.seconds(0.3),
                            new KeyValue(particle.radiusProperty(), 3 * config.getSize())
                    ),
                    new KeyFrame(Duration.seconds(1.2 / config.getSpeed()),
                            new KeyValue(particle.centerXProperty(), x + Math.cos(Math.toRadians(angle)) * distance),
                            new KeyValue(particle.centerYProperty(), y + Math.sin(Math.toRadians(angle)) * distance),
                            new KeyValue(particle.opacityProperty(), 0.0),
                            new KeyValue(particle.radiusProperty(), 0.5 * config.getSize())
                    )
            );

            // 添加拖尾效果
            ParallelTransition parallel = new ParallelTransition();
            parallel.getChildren().add(particleTimeline);

            // 创建闪烁效果
            Timeline blinkTimeline = new Timeline(
                    new KeyFrame(Duration.seconds(0.1), e -> particle.setOpacity(0.3)),
                    new KeyFrame(Duration.seconds(0.2), e -> particle.setOpacity(1.0))
            );
            blinkTimeline.setCycleCount(3);
            parallel.getChildren().add(blinkTimeline);

            parallel.setOnFinished(e -> container.getChildren().remove(particle));
            container.getChildren().add(particle);
            parallel.play();
        }
    }
}